﻿using System;
using System.Configuration;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace TMS
{
    public partial class PartnersControl : UserControl
    {
        private bool listOfPartnersLoaded = false;
        public MySqlDataAdapter MyDA = new MySqlDataAdapter();
        DataSet table = new DataSet();


        public PartnersControl()
        {
            Program.GetMainForm.Text = "TMS - Комитенти";
            InitializeComponent();

            if (!listOfPartnersLoaded)
            {
                loadListOfPartners();
                listOfPartnersLoaded = true;
            }
        }

        private void loadListOfPartners()
        {
            string connString = ConfigurationManager.ConnectionStrings["MyDBConnectionString"].ConnectionString;

            MySqlConnection mysqlCon = new MySqlConnection(connString);
            mysqlCon.Open();


            string sqlSelectAll = "SELECT * FROM Partners";
            
            MyDA.SelectCommand = new MySqlCommand(sqlSelectAll, mysqlCon);

            //nov kod
            MyDA.Fill(table);

            dgvPartnersList.DataSource = table.Tables[0];

            dgvPartnersList.Columns[0].Visible = false;//id
            dgvPartnersList.Columns[1].HeaderText = "Име";
            dgvPartnersList.Columns[2].HeaderText = "Адреса";
            dgvPartnersList.Columns[3].HeaderText = "Пош.број";
            dgvPartnersList.Columns[4].HeaderText = "Град";
            dgvPartnersList.Columns[5].HeaderText = "Телефон";
            dgvPartnersList.Columns[6].HeaderText = "Сметка";
            dgvPartnersList.Columns[7].HeaderText = "Држава";
            dgvPartnersList.Columns[8].HeaderText = "E-mail";
            dgvPartnersList.Columns[9].HeaderText = "Лице за контак";
            
            
        }

       

        private void dgvPartnersList_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            DataGridViewRow row = dgvPartnersList.CurrentRow;
            Program.GetMainForm.ShowControl(ControlsEnum.CREATE_EDIT_PARTNER_CONTROL, row);
            this.Dispose();
        }

        private void txtSearch_TextChanged(object sender, EventArgs e)
        {
            table.Tables[0].DefaultView.RowFilter = string.Format("Name LIKE'" + ToStSearch.Text + "*'");
            dgvPartnersList.Update();

        }

        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            this.Dispose();
            Program.GetMainForm.registerInHistoryMain(ControlsEnum.CREATE_EDIT_PARTNER_CONTROL,null);
            Program.GetMainForm.ShowControl(ControlsEnum.CREATE_EDIT_PARTNER_CONTROL);
        }

        private void ToStBack_Click(object sender, EventArgs e)
        {
            if (Program.GetMainForm.Back())
                this.Dispose();
        }


        private void ToStForward_Click(object sender, EventArgs e)
        {
            if (Program.GetMainForm.Forward())
                this.Dispose();
        }
    }
}
